package com.example.springboot.controller;

import com.example.springboot.service.GoodsService;
import com.example.springboot.document.GoodsDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @Description: GoodsController
 * @Author wenlinyu
 * @Date: 2020/12/21 16:16
 * @Version 1.0
 */
@Api(tags = "商品API")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 按id获取商品信息
     */
    @ApiOperation(value = "根据id获取商品信息")
    @GetMapping("/goods/{id}")
    public GoodsDo getOne(@PathVariable("id") long id) {
        Optional<GoodsDo> opt = goodsService.getById(id);
        return opt.get();
    }

    /**
     * 获取商品列表
     */
    @ApiOperation(value = "获取商品列表")
    @GetMapping("/goods")
    public Iterable<GoodsDo> getList() {
        return goodsService.getList();
    }

    /**
     * 新增商品
     */
    @ApiOperation(value = "新增商品")
    @PostMapping("/goods")
    public GoodsDo add(@RequestBody GoodsDo goods) {
        return goodsService.add(goods);
    }

    /**
     * 编辑商品
     */
    @ApiOperation(value = "根据Id修改商品信息")
    @PutMapping("/goods/{id}")
    public GoodsDo update(@PathVariable("id") long id, @RequestBody GoodsDo goods) {
        // 修改指定id的博客信息
        goods.setId(id);
        return goodsService.edit(goods);
    }

    /**
     * 移除商品
     */
    @ApiOperation(value = "根据id删除商品")
    @DeleteMapping("/goods/{id}")
    public void delete(@PathVariable("id") long id) {
        goodsService.remove(id);
    }
}
